package com.oocl.batchdatav2.server;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

import com.oocl.batchdatav2.action.DataProcessAction;
import com.oocl.batchdatav2.msghandler.MessageLooper;

public class TCPServer {

	private int port;
	private ServerSocket server;
	private Socket socket;
	private DataProcessAction action = DataProcessAction.INSTANCE;
	
	public TCPServer(int port) throws IOException {
		this.port = port;
		action.loadDataFromFile("data.txt");
	}
	
	public void start() throws Exception {
		server = new ServerSocket(port);
		socket = server.accept();
		
		InputStream in = socket.getInputStream();
		OutputStream out = socket.getOutputStream();
		
		new MessageLooper(in, out).startLoop();
	}
	
	public void stop() throws IOException {
		socket.close();
		server.close();
	}
	
}
